package com.ds.dsm.aggregation.config.entity.service;

import com.ds.config.ErrorResultModel;
import com.ds.config.ResultModel;
import com.ds.context.JDSActionContext;
import com.ds.editor.ESDEditor;
import com.ds.editor.cmd.ESDChrome;
import com.ds.esd.client.ESDFacrory;
import com.ds.esd.custom.CustomViewFactory;
import com.ds.esd.custom.annotation.CustomAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.api.enums.RequestPathEnum;
import com.ds.esd.custom.bean.ApiClassConfig;
import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.aggregation.AggregationManager;
import com.ds.esd.dsm.aggregation.DomainInst;
import com.ds.esd.dsm.view.ViewEntityConfig;
import com.ds.esd.dsm.view.ViewInst;
import com.ds.esd.dsm.view.ViewManager;
import com.ds.esd.tool.module.EUModule;
import com.ds.esd.tool.ui.enums.ComponentType;
import com.ds.esd.tool.ui.module.ModuleComponent;
import com.ds.web.annotation.Aggregation;
import com.ds.web.annotation.AggregationType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(path = "/dsm/agg/config/action/")
@Aggregation(type = AggregationType.menu, rootClass = AggBuildAction.class)
public class AggBuildAction {


    @RequestMapping(value = {"javaBuild"}, method = {RequestMethod.POST})
    @APIEventAnnotation()
    @CustomAnnotation(index = 0, caption = "编译", imageClass = "spafont spa-icon-coin")
    @ResponseBody
    public ResultModel<Boolean> build(String domainId, String sourceClassName) {
        ResultModel resultModel = new ResultModel();
        ESDChrome chrome = getCurrChromeDriver();
        try {
            AggregationManager aggregationManager = DSMFactory.getInstance().getAggregationManager();
            DomainInst domainInst = aggregationManager.getDomainInstById(domainId);
            aggregationManager.buildAggEntity(domainInst, sourceClassName, chrome);
            DSMFactory.getInstance().compileDsmInst(domainInst, this.getCurrChromeDriver());
            DSMFactory.getInstance().reload();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultModel;
    }

    @RequestMapping(value = {"buildView"}, method = {RequestMethod.POST})
    @APIEventAnnotation(customRequestData = {RequestPathEnum.SPA_projectName})
    @CustomAnnotation(index = 1, caption = "编译视图", imageClass = "spafont spa-icon-moveforward")
    @ResponseBody
    public ResultModel<Boolean> buildView(String domainId, String sourceClassName) {
        ResultModel resultModel = new ResultModel();
        ESDChrome chrome = getCurrChromeDriver();
        try {
            AggregationManager aggregationManager = DSMFactory.getInstance().getAggregationManager();
            ViewManager viewManager = DSMFactory.getInstance().getViewManager();
            DomainInst domainInst = aggregationManager.getDomainInstById(domainId);
            aggregationManager.buildAggEntity(domainInst, sourceClassName, chrome);
            List<ViewInst> viewInsts = viewManager.getViewInstList(domainInst.getProjectVersionName(), domainId);
            for (ViewInst viewInst : viewInsts) {
                ViewEntityConfig viewEntityConfig = viewManager.getViewEntityConfig(sourceClassName, viewInst.getViewInstId());
                ApiClassConfig sourceConfig = viewEntityConfig.getSourceConfig();

                for (MethodConfig methodAPIBean : sourceConfig.getAllViewMethods()) {
                    EUModule module = ESDFacrory.getESDClient().getModule(methodAPIBean.getUrl(), domainInst.getProjectVersionName());
                    if (module != null) {
                        List<ModuleComponent> components = module.getComponent().findComponents(ComponentType.Module, null);
                        for (ModuleComponent moduleComponent : components) {
                            ESDFacrory.getESDClient().delModule(moduleComponent.getEuModule());
                        }
                        ESDFacrory.getESDClient().delModule(module);
                    }
                    viewManager.buildView(methodAPIBean, viewInst.getViewInstId(), chrome);
                    CustomViewFactory.getInstance().buildView(methodAPIBean, domainInst.getProjectVersionName(), (Map<String, ?>) JDSActionContext.getActionContext().getContext(), true);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            chrome.printError(e.getMessage());
            ((ErrorResultModel) resultModel).setErrdes(e.getMessage());
        }
        return resultModel;
    }

    public ESDChrome getCurrChromeDriver() {
        Object handleId = JDSActionContext.getActionContext().getParams("handleId");
        ChromeDriver chrome = null;
        if (handleId != null) {
            chrome = ESDEditor.getInstance().getChromeDriverById(handleId.toString());
        }
        if (chrome == null) {
            chrome = ESDEditor.getInstance().getCurrChromeDriver();
        }
        return new ESDChrome(chrome);
    }

}

